package com.bilibili.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bilibili.dao.BaseDao;
import com.bilibili.dao.FollowDao;
import com.bilibili.entity.Comment;
import com.bilibili.entity.Follow;

public class FollowDaoImpl implements FollowDao {

	@Override
	public int SelectFollower(int followeeId) {
		Connection conn=BaseDao.getConn();
		PreparedStatement s = null;
		ResultSet rs = null;
		try {
			if(conn!=null) {
				
			}
			String sql = "SELECT COUNT(*) AS total FROM follow WHERE followee_id= "+ followeeId;
			s = conn.prepareStatement(sql);
			rs =  s.executeQuery();
			while(rs.next()) {
				int total = rs.getInt("total");
				return total;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {			
			BaseDao.closeAll(conn, s,rs);
		}
		return 0;
	}
	public int insertMyAttention(Follow follow) {
		String sql = "INSERT INTO follow VALUE(?,?,NOW())";
		Object[] param = {follow.getFollowerId(),follow.getFolloweeId()};
		return BaseDao.executeUpdate(sql, param);
	}

	@Override
	public int deleteMyAttention(Follow follow) {
		// TODO Auto-generated method stub
		String sql = "delete from follow where follower_id = ? and followee_id = ?";
		Object[] param = {follow.getFollowerId(),follow.getFolloweeId()};
		return BaseDao.executeUpdate(sql, param);
	}
	@Override
	public Follow selectUserIsFollower(Follow follow) {
		// TODO Auto-generated method stub
		Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "SELECT * FROM follow WHERE follower_id= ? AND followee_id= ?";
        Follow f = null;
        try {
            con = BaseDao.getConn();
            ps = con.prepareStatement(sql);
            ps.setInt(1, follow.getFollowerId());
            ps.setInt(2, follow.getFolloweeId());
            rs = ps.executeQuery();
            while (rs.next()) {
                f = new Follow();
                f.setFolloweeId(rs.getInt("followee_id"));
                f.setFollowerId(rs.getInt("follower_id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeAll(con, ps, rs);
        }
        return f;
	}

}
